package com.example.ss12.childtracker;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {
	private static final String TAG = "MyService";
	LocationPostData sendLocationPost = new LocationPostData("", 0.0 , 0.0);
	private Timer timer = new Timer();
	TimerTask scanTask;
	final Handler handler = new Handler();
	
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
	@Override
	public void onCreate() {
		Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
	}
	
	private class SendLoc extends AsyncTask<LocationPostData, Integer, Integer> {
	     protected Integer doInBackground(LocationPostData...toSend) {
	    	 LocationPostData dataToSend = toSend[0];	    
	    	 try {
		        HttpClient client = new DefaultHttpClient();  
		        HttpPost post = new HttpPost(dataToSend.urlAddress);
		            List<NameValuePair> params = new ArrayList<NameValuePair>();
		            params.add(new BasicNameValuePair("longitude", Double.toString(dataToSend.longitude)));
		            params.add(new BasicNameValuePair("latitude",  Double.toString(dataToSend.latitude)));
		            UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
		            post.setEntity(ent);
		            HttpResponse responsePOST = client.execute(post);  
		            HttpEntity resEntity = responsePOST.getEntity();  
		            if (resEntity != null) {    
		                //Log.("RESPONSE",EntityUtils.toString(resEntity));
		            }
		    } catch (Exception e) {
		        e.printStackTrace();
		    }
	    	 return 0;
	     }

	 }

	@Override
	public void onDestroy() {
		Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
		Log.d(TAG, "onDestroy");
	}
	
	@Override
	public void onStart(Intent intent, int startid) {
		Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
		Log.d(TAG, "onStart");

		scanTask = new TimerTask() {
	        public void run() {
	                handler.post(new Runnable() {
	                        public void run() {
	            				Toast.makeText(MyService.this, "Timertask", Toast.LENGTH_LONG).show();
	            				LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
	            				ShowLocation gpsUpload = new ShowLocation(locationManager);
	            				sendLocationPost = gpsUpload.upLoadLocation();
	            				new SendLoc().execute(sendLocationPost);
	                        }
	               });
	        }};
	    timer.schedule(scanTask, 0, 5000); 
		
		
		/*
		LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		ShowLocation gpsUpload = new ShowLocation(locationManager);
		sendLocationPost = gpsUpload.upLoadLocation();
		new SendLoc().execute(sendLocationPost);
		*/
	}
}